#import <string.h>
#import <ctype.h>

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIColor.h>

#import "TextView.h"
#import "QtKey.h"

typedef enum
{
	UITextAutocapitalizationTypeNone,
	UITextAutocapitalizationTypeWords,
	UITextAutocapitalizationTypeSentences,
	UITextAutocapitalizationTypeAllCharacters,
} UITextAutocapitalizationType;

typedef enum
{
	UIKeyboardAppearanceDefault,
	UIKeyboardAppearanceAlert,
} UIKeyboardAppearance;

typedef enum
{
	UIReturnKeyDefault,
	UIReturnKeyGo,
	UIReturnKeyGoogle,
	UIReturnKeyJoin,
	UIReturnKeyNext,
	UIReturnKeyRoute,
	UIReturnKeySearch,
	UIReturnKeySend,
	UIReturnKeyYahoo,
	UIReturnKeyDone,
	UIReturnKeyEmergencyCall,
} UIReturnKeyType;

@implementation TextView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
	if ((self = [super initWithFrame:frame]) == nil)	return nil;
	delegate = nil;
	[self setAutocapitalizationType:UITextAutocapitalizationTypeNone];
	[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
	[self setReturnKeyType:UIReturnKeyDefault];
	return self;
}

// http://ericasadun.com/iPhoneDocs220/_u_i_text_view_8h-source.html
- (BOOL)keyboardInputShouldDelete:(id)fp8
{
	if (![self hasText])	// send backspace when text is empty
		[delegate qtKeyClicked:Key_Backspace];
	return [super keyboardInputShouldDelete:fp8];
}

- (BOOL)keyboardInput:(id)fp8 shouldInsertText:(id)text isMarkedText:(BOOL)fp16
{
	if (strlen([text cStringUsingEncoding:NSUTF8StringEncoding]) == 1)
	{
		char c = [text cStringUsingEncoding:NSUTF8StringEncoding][0];
		
		if (isalnum(c))
		{
			[delegate qtKeyClicked:toupper(c)];
			return NO;
		}
		
		switch (c)
		{
			case '-':	[delegate qtKeyClicked:Key_Minus];	return NO;
			case '/':	[delegate qtKeyClicked:Key_Slash];	return NO;
			case ':':	[delegate qtKeyClicked:Key_Colon];	return NO;
			case ';':	[delegate qtKeyClicked:Key_Semicolon];	return NO;
			case '(':	[delegate qtKeyClicked:Key_ParenLeft];	return NO;
			case ')':	[delegate qtKeyClicked:Key_ParenRight];	return NO;
			case '$':	[delegate qtKeyClicked:Key_Dollar];	return NO;
			case '&':	[delegate qtKeyClicked:Key_Ampersand];	return NO;
			case '@':	[delegate qtKeyClicked:Key_At];	return NO;
			case '\"':	[delegate qtKeyClicked:Key_QuoteDbl];	return NO;
			case '.':	[delegate qtKeyClicked:Key_Period];	return NO;
			case ',':	[delegate qtKeyClicked:Key_Comma];	return NO;
			case '?':	[delegate qtKeyClicked:Key_Question];	return NO;
			case '!':	[delegate qtKeyClicked:Key_Exclam];	return NO;
			case '\'':	[delegate qtKeyClicked:Key_Apostrophe];	return NO;
			case '[':	[delegate qtKeyClicked:Key_BracketLeft];	return NO;
			case ']':	[delegate qtKeyClicked:Key_BracketRight];	return NO;
			case '{':	[delegate qtKeyClicked:Key_BraceLeft];	return NO;
			case '}':	[delegate qtKeyClicked:Key_BraceRight];	return NO;
			case '#':	[delegate qtKeyClicked:Key_NumberSign];	return NO;
			case '%':	[delegate qtKeyClicked:Key_Percent];	return NO;
			case '^':	[delegate qtKeyClicked:Key_AsciiCircum];	return NO;
			case '*':	[delegate qtKeyClicked:Key_Asterisk];	return NO;
			case '+':	[delegate qtKeyClicked:Key_Plus];	return NO;
			case '=':	[delegate qtKeyClicked:Key_Equal];	return NO;
			case '_':	[delegate qtKeyClicked:Key_Underscore];	return NO;
			case '\\':	[delegate qtKeyClicked:Key_Backslash];	return NO;
			case '|':	[delegate qtKeyClicked:Key_Bar];	return NO;
			case '~':	[delegate qtKeyClicked:Key_AsciiTilde];	return NO;
			case '<':	[delegate qtKeyClicked:Key_Less];	return NO;
			case '>':	[delegate qtKeyClicked:Key_Greater];	return NO;
				break;
			case 0x0a:
				if ([self hasText])
				{
					[delegate sendingTextRequested:[self text]];
					[self setText:@""];
				}
				else
				{
					[delegate qtKeyClicked:Key_Return];
				}
				return NO;
			case ' ':
				if (![self hasText])
				{
					[delegate qtKeyClicked:Key_Space];
					return NO;
				}
			default:
				break;
		}
	}
	
	return [super keyboardInput:fp8 shouldInsertText:text isMarkedText:fp16];
}

@end
